#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int deleteAndEarn(vector<int>& nums) {
        int n = nums.size();
        sort(nums.begin(), nums.end());
        int m = nums[n - 1];
        vector<int>v(m + 1);
        for (auto e : nums)
        {
            v[e] += e;
        }
        vector<int>dpf(m + 1);
        auto dpg = dpf;
        dpf[0] = v[0];
        for (int i = 1; i <= m; i++)
        {
            dpf[i] = dpg[i - 1] + v[i];
            dpg[i] = max(dpg[i - 1], dpf[i - 1]);
        }
        return max(dpf[m], dpg[m]);
    }
};